package redis;

/**
 * @Author :
 * @Description :
 * @Date : Created in 16:27 2017/11/23
 * @Modified By : NONE
 */

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;


//自定义starter,
        /*pom:引入父类pom,redis依赖，nexus私服设置
        加入相关配置类 @Bean @Component
        META_INF 配置spring.factories

        其他服务只需引入该starter的pom即可，必须先把本starter   mvn deploy到私服，其他服务在刷新maven*/



//各个服务分开解决 引入不了其他服务pom的问题，加入nexus

//nexus
//localhost:8081/nexus -->admin admin123
//maven settings.xml中设置
// <!--配置远程仓库认证信息-->
//     </servers>
//	     <server>
//             <id>snapshots</id>
//             <username>admin</username>
//             <password>admin123</password>
//         </server>
//      </servers>
//代码pom配置
// <!--将ssm_dao上传私服  -->
//    <distributionManagement>
//            <!--pom.xml这里<id> 和 settings.xml 配置 <id> 对应  -->
//            <snapshotRepository>
//            <id>snapshots</id>
//            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
//            </snapshotRepository>
//     </distributionManagement>
//注意此时需要配置ideamaven的仓库及配置文件
//mvn  deploy即可上传jar包，项目中引入加入pom刷新即可
//
//
//
@Configuration
public class RedisConfiguration {

    @Bean
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisFactory){
        StringRedisTemplate template = new StringRedisTemplate(redisFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
                Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}